package UIFree {
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	
	public class uif_ecomponent extends uif_component {
		public static const DISPOSE: String 	= 'DISPOSE';
		public static const CHANGEINDEX: String = 'CHANGEINDEX';
		
		private var _events: Array;
		public function uif_ecomponent(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass: Object='') {
			_events = new Array();
			super(parent, ax, ay, awidth, aheight, styleClass);
			listenEvents();
		}
		
		protected function listenEvents(): void {
		}
		
		public function trace(aMsg: Object): void {
			if (app) app.dispatchEvent(new ObjectEvent('TRACE', aMsg));
		}
		
		public function addEvent(a_eventType: String, func: Function, a_Dispatcher: IEventDispatcher=null): int {
			var _setFunc: Function = func;
			if ((a_eventType == MouseEvent.CLICK) || (a_eventType == MouseEvent.DOUBLE_CLICK)) {
				_setFunc = function (e: MouseEvent): void {
					if (!disable) func(e);
				}
			}
			var index: int = indexOfE(a_eventType, _setFunc, a_Dispatcher);
			if (index == -1) {
				(a_Dispatcher?a_Dispatcher:super).addEventListener(a_eventType, _setFunc);
				_events.push({eventType: a_eventType, eventFunc: func, dFunc: _setFunc, dispatcher: a_Dispatcher});
				return _events.length - 1;
			} else return index;
		}
		
		public function removeEvent(index: int): void {
			if ((index > -1) && (index < _events.length)) {
				var info: Object = _events[index];
				if (info.dispatcher) info.dispatcher.removeEventListener(info.eventType, info.dFunc);
				else super.removeEventListener(info.eventType, info.dFunc);
				_events.splice(index, 1);
			}
		}
		
		public function indexOfE(type: String, listener:Function, a_Dispatcher: IEventDispatcher=null): int {
			for (var i:int=0; i<_events.length; i++)
				if ((_events[i].eventType == type) &&
					(_events[i].eventFunc == listener) && 
					(_events[i].dispatcher == a_Dispatcher)) return i;
			return -1;
		}
		
		override public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void {
			var eIndex: int = indexOfE(type, listener);
			if (eIndex > -1) removeEvent(eIndex);
			else super.removeEventListener(type, listener);
		}
		
		public function oneEvent(type: String, listener: Function, a_Dispatcher: EventDispatcher=null): void {
			function onEvent(e: Event): void {
				removeEvent(indexOfE(type, onEvent, a_Dispatcher));
				listener(e);
			}
			var index: int = addEvent(type, onEvent, a_Dispatcher);
		}
		
		public function clearEvents(): void {
			while (_events.length > 0) removeEvent(0);
		}
		
		public function get eventSpace(): EventDispatcher {
			return app;
		}
		
		
		public function get index(): int {
			return parent?parent.getChildIndex(this):-1;
		}
		
		public function set index(a_index: int): void {
			if (parent && (a_index != index))
				setIndex(a_index);
		}
		
		protected function setIndex(a_index: int): void {
			if (a_index != index) {
				parent.setChildIndex(this, a_index);
				dispatchEvent(new Event(CHANGEINDEX));
			}
		}
		
		public function disposeChilds(): void {
			forEach(function(child: uif_ecomponent): void {
				child.dispose();
			}, uif_ecomponent);
/*			var i: int = 0;
			while (i < numChildren) {
				var child: Object = getChildAt(i);
				if (child is uif_ecomponent) {
					(child as uif_ecomponent).dispose();					
				} else i++;
			}*/
		}
		
		public function dispose(): void {
			dispatchEvent(new Event(DISPOSE));
			clearEvents();
			disposeChilds();
			if (parent) parent.removeChild(this);
		}
	}
}